package com.central.apps.handler.MaterialInventory;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.entity.BlInsparea;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.app.common.util.BLuntil;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 *
 * 入库管理
 */
@Component
public class instorageHandler extends LvBaseHandler {

    @Autowired
    private HandlerService handlerService;

    /**
     * 自定义审批人
     * @param app
     * @param id
     * @param flowNode
     * @param vars
     * @return
     * @throws Exception
     */
    public String addFlowCandidate(String app, Long id, FlowNode flowNode, Map<String, Object> vars) throws Exception {
        //定义一个存放人员的字符串
        String executer = null;
        List<Map<String, Object>> list = handlerService.findBySql("select leadernum,managernum from gq_warehouse where warehousenum =(select warehousenum from gq_instorage where id='"+id+"' )");
//        String leadernum = MapUtil.getStr(list.get(0), "leadernum");//仓库经理
        String managernum= MapUtils.getString(list.get(0),"managernum");//仓库管理员


        //仓库管理员
        if (flowNode.getId().equals("gly")) {
            if (managernum!=null&&!managernum.equals("")){
                executer=managernum;
            }
        }
//        //仓库经理
//        if (flowNode.getId().equals("jl")) {
//            if (managernum!=null&&!managernum.equals("")){
//                executer=managernum;
//            }
//        }
        return executer;
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if(flowNode.getId().equals("fqr")) {
            boolean flag = op.isFlag();//判断是否审核通过
            List<Map<String, Object>> list = handlerService.findBySql("select * from gq_instorage where id="+id);
            String ponum =MapUtil.getStr(list.get(0),"ponum");
            if(flag){
                // 发起流程后修改采购单状态
                handlerService.updateBySql("update gq_cgdan set isruku='是' where cgpaynum='"+ponum+"'");
            }else{
                // 作废 释放采购单
                handlerService.updateBySql("update gq_cgdan set isruku='否' where cgpaynum='"+ponum+"'");

            }


        }

            if(flowNode.getId().equals("gly")){
            //审核通过后增加库存
            boolean flag = op.isFlag();//判断是否审核通过
            //审核通过的情况
            if(flag) {
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_instorage where id="+id);
                String instonum = BLuntil.getParameter(list.get(0), "instonum");//入库单号
                String warehousenum = BLuntil.getParameter(list.get(0), "warehousenum");//仓库编码
                String warehousendesc = BLuntil.getParameter(list.get(0), "warehousedesc");//仓库描述
                String orgcode = BLuntil.getParameter(list.get(0), "orgcode");//总公司编码
                String sitecode = BLuntil.getParameter(list.get(0), "sitecode");//分公司编码
                String deptcode = BLuntil.getParameter(list.get(0), "deptcode");//部门编码

                List<Map<String, Object>> lineList = handlerService.findBySql("select *from gq_instorageitem where instonum ='"+instonum+"'");
                if (lineList != null) {
                    for (int k = 0; k < lineList.size(); k++) {
                        String itemNUm= MapUtil.getStr(lineList.get(k), "itemid");//物资编码
                        String itemdesc= MapUtil.getStr(lineList.get(k), "itemdesc");//物资描述
//                        String instonum= MapUtil.getStr(lineList.get(k), "instonum");//入库编号
                        Integer innum= MapUtil.getInt(lineList.get(k), "innum");//入库数量

                        List<Map<String, Object>> inventorysList = handlerService.findBySql("select * from gq_Inventorys  where itemnum='"+itemNUm+"' and warehousenum ='"+warehousenum+"'");
                        Integer currentqty= innum;//可用库存
                        Integer actqty= innum;//实际库存
                        String  shelfnum= BLuntil.getParameter(lineList.get(k), "shelfnum");//库位编号
                        String  shelfdesc= BLuntil.getParameter(lineList.get(k), "shelfdesc");//库位描述
                        String  price= BLuntil.getParameter(lineList.get(k), "inprice") ;//单价
                        List<Map<String, Object>> invbalanceList = handlerService.findBySql("select * from gq_Invbalance  where warehousenum='"+warehousenum+"' and areanum ='"+shelfnum+"'");

                        if(inventorysList!=null && inventorysList.size()>0){
                            currentqty+=MapUtil.getInt(inventorysList.get(0), "currentqty");
                            actqty+=MapUtil.getInt(inventorysList.get(0), "actqty") ;
                                    // 更新库存数量
                            handlerService.findBySql("update  gq_Inventorys  set currentqty="+currentqty+" ,actqty="+actqty+" where itemnum='"+itemNUm+"' and warehousenum ='"+warehousenum+"'");
                            if(invbalanceList!=null && invbalanceList.size()>0) {
                                // 更新库位余量
                                handlerService.findBySql("update  gq_Invbalance  set currentqty="+currentqty+" ,actqty="+actqty+" where areanum='"+shelfnum+"' and warehousenum ='"+warehousenum+"'");
                            }else{
                                // 插入库位
                                handlerService.findBySql("insert into gq_Invbalance (areanum,areadesc,currentqty,actqty,inprice,warehousenum) values ('"+shelfnum+"','"+shelfdesc+"','"+currentqty+"','"+actqty+"','"+price+"','"+warehousenum+"')");
                            }
                        }else{
                            // 插入库存
                            handlerService.findBySql("insert into gq_inventorys (orgcode,sitecode,deptcode,itemnum,warehousenum,currentqty,actqty,itemdesc,warehousedesc,innum) values ('"+orgcode+"','"+sitecode+"','"+deptcode+"','"+itemNUm+"','"+warehousenum+"','"+innum+"','"+innum+"','"+itemdesc+"','"+warehousendesc+"','0')");
                            // 插入库位
                            handlerService.findBySql("insert into gq_Invbalance (areanum,areadesc,currentqty,actqty,inprice,warehousenum,itemnum) values ('"+shelfnum+"','"+shelfdesc+"','"+currentqty+"','"+actqty+"','"+price+"','"+warehousenum+"','"+itemNUm+"')");

                        }
                    }
                }

            }

        }
    }

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        //库位接口
        if("Invbalance".equals(type)){
            String warehousenum = MapUtil.getStr(params, "warehousenum");//仓库编码
            String sql =" select * from gq_areashelf where warehousenum ='"+warehousenum+"'";
            return BLuntil.toPage(params,handlerService,sql);

        }
        //采购单行接口
        if("getPoLine".equals(type)){
            String ponum = MapUtil.getStr(params, "ponum");//采购单号
            String sql =" select * from gq_cgdanitem where cgpaynum ='"+ponum+"'";
            return BLuntil.toPage(params,handlerService,sql);

        }

        return super.operate(app, type, params);
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        String warehousenum=BLuntil.getParameter(data,"warehousenum");
        String instonum=BLuntil.getParameter(data,"instonum");
        handlerService.findBySql("update gq_instorageitem set warehousenum='"+warehousenum+"' where instonum='"+instonum+"' ");

        super.afterSave(app, objName, data, isUpdate);
    }
}
